<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-table v-loading="loading" :data="roomNormList" >
        <el-table-column label="收费项目类型" align="center" prop="projectType" width="150" />
        <el-table-column label="收费项目代码" align="center" prop="chargeCode" width="150" />
        <el-table-column label="收费项目名称" align="center" prop="chargeName" width="150" />
        <el-table-column label="收取" align="center" prop="status" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="Y"
              inactive-value="N">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="收费标准" align="center" prop="normId" width="200">
          <template slot-scope="scope">
            <el-select v-model="scope.row.normId" :disabled="scope.row.status!='Y'">
              <el-option
                v-for="dict in normOptions"
                :key="dict.normId"
                :label="dict.normNames"
                :value="dict.normId"
                v-if="scope.row.chargeId==dict.chargeId"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="开始收费时间" align="center" width="230">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.beginChargeTime"
              type="date"
              placeholder="选择开始收费时间"
              :disabled="scope.row.status!='Y'"
              value-format="yyyy-MM-dd"
              :clearable="false"
              size="small"
              clearable
            >
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="结束收费时间" align="center" width="230">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.finishChargetime"
              type="date"
              placeholder="选择结束收费时间"
              :disabled="scope.row.status!='Y'"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
            >
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="指定金额" align="center" width="230">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.amountSpecified"
              size="small"
              :precision="2"
              :step="0.1"
              :min="0"
              :disabled="scope.row.status!='Y'"
            />
          </template>
        </el-table-column>
        <!--<el-table-column label="违约金计算方式" align="center" prop="prices" />-->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { addRoomNorm } from "@/api/payfee/roomNorm";
import { listItem } from '@/api/payfee/item';
import { listNorm } from '@/api/payfee/norm';
import { getFloor } from "@/api/property/floor";
import { getRoom } from "@/api/property/room";
import { getUnit } from "@/api/property/unit";
export default {
  name: 'roomNormAdd',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 收费标准表格数据
      roomNormList: [],
      // 弹出层标题
      title: '房屋收费标准设置',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {

      },
      parentParam:{},
      //收费标准字典
      normOptions:[],
      //小区ID
      communityId:null
    };
  },
  created() {

  },
  methods: {
    show(option){
      this.parentParam = option
      if(option.currentNode.modelType == 'community'){
        this.communityId = option.currentNode.id
        this.getNormOptions();
      }else if(option.currentNode.modelType == 'floor'){
        getFloor(option.currentNode.id).then(res=>{
          this.communityId = res.data.communityId
          this.getNormOptions();
        })
      }else if(option.currentNode.modelType == 'unit'){
        getUnit(option.currentNode.id).then(res=>{
          this.communityId = res.data.communityId
          this.getNormOptions();
        })
      }else if(option.currentNode.modelType == 'room'){
        getRoom(option.currentNode.id).then(res=>{
          this.communityId = res.data.communityId
          this.getNormOptions();
        })
      }
      this.open = true
      this.getList()
    },
    /** 查询收费标准列表 */
    getList() {
      this.loading = true;
      listItem({projectIdIN:"'01','02','05'"}).then(response => {
        let roomNormList = response.rows;
        for(let i in roomNormList){
          let row = roomNormList[i]
          row['status'] = 'N';
          row['beginChargeTime'] = null;
          // row['beginChargeTime'] = this.parseTime(new Date());
          row['finishChargetime'] = null;
          row['amountSpecified'] = '0.00';
        }
        this.roomNormList = roomNormList;
        this.loading = false;
      });
    },
    getNormOptions(){
      listNorm({deptId:this.communityId}).then(res=>{
        this.normOptions = res.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.$emit("cancel")
    },
    /** 提交按钮 */
    submitForm() {
      let fruitList = []
      for(let i in this.roomNormList){
        let row = this.roomNormList[i];
        if(row.status == 'Y'){
          if(!row.normId){
            this.msgError(`收费项目【${row.chargeName}】收费标准不能为空!`);
            return;
          }
          fruitList.push({
            projectId:row.projectId,
            chargeId:row.chargeId,
            normId:row.normId,
            beginChargeTime:row.beginChargeTime,
            finishChargetime:row.finishChargetime,
            amountSpecified:row.amountSpecified
          })
        }
      }
      addRoomNorm({'nodeId' : this.parentParam.currentNode.id , 'modelType' : this.parentParam.currentNode.modelType , 'roomNorms' : fruitList}).then(res=>{
        this.msgSuccess("新增房屋收费标准成功！");
        this.cancel();
      })
    },
  }
};
</script>
